package com.base.study.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.bean.StudyEnent;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.study.R;
import com.base.study.adapter.MyStudyListAdapter;
import com.base.study.adapter.MyStudyRecyclerAdapter;
import com.base.study.bean.OnlineStudyListBean;
import com.base.study.bean.ScopeBean;
import com.base.study.databinding.FragmentMyStudyBinding;
import com.base.study.presenter.MyStudyListPresenter;
import com.base.study.presenter.OnlineStudyListPresenter;
import com.base.study.view.MyStudyListView;
import com.base.study.view.OnlineStudyListView;
import com.base.study.widget.ConfirmPopWindow;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * 我的学习
 * Created by zw on 2020/2/13.
 */

public class MyStudyFragment extends CBaseMvpFragment<MyStudyListPresenter> implements MyStudyListView {
    private Disposable disposable;
    private FragmentMyStudyBinding mBinding;
    private List<OnlineStudyListBean> mData = new ArrayList<>();
    private int page = 1;
    private ScopeBean mScopeBean = new ScopeBean();
    //private MyStudyRecyclerAdapter wrapperListAdapter;
    private int scope;
    private MyStudyListAdapter myStudyListAdapter;

    @Override
    public void init() {
        mBinding = (FragmentMyStudyBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter = new MyStudyListPresenter(this, getActivity());
        mPresenter.start();
    }

    @Override
    public void start() {
        initRecyClerView();
        // mPresenter.getList(page,10);
        mPresenter.getScope("edu");
    }


    private void initRecyClerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(linearLayoutManager);
        mBinding.rv.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 0, 10, 10));
        myStudyListAdapter = new MyStudyListAdapter(mContext, getActivity(),mData,scope,mPresenter.mUser.getTenantId(),mPresenter.mUser.getDeptId());
        mBinding.rv.setAdapter(myStudyListAdapter);
        myStudyListAdapter.isHead(0);
       // setHeaderView(mBinding.rv);
        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getScope("edu");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getScope("edu");
            }
        });
//        disposable = RxBus2.getInstance().doSubscribe(StudyEnent.class, new Consumer<StudyEnent>() {
//            @Override
//            public void accept(StudyEnent bean) throws Exception {
//                if (mPresenter != null) {
//                    page = 1;
//                    mPresenter.getScope("edu");
//                }
//            }
//        });

    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my_study;
    }


    @Override
    public void returnNoData() {
        myStudyListAdapter.setScope(scope);
        mBinding.smartRefreshLayout.finishRefresh();
        //mBinding.empty.setVisibility(View.VISIBLE);
        if (myStudyListAdapter != null) {
            myStudyListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void returnList(List<OnlineStudyListBean> bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(bean);
        myStudyListAdapter.setScope(scope);
        if (myStudyListAdapter != null) {
            myStudyListAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnMoreList(List<OnlineStudyListBean> bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(bean);
        myStudyListAdapter.setScope(scope);
        if (myStudyListAdapter != null) {
            myStudyListAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnNoMoreData() {
        myStudyListAdapter.setScope(scope);
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        myStudyListAdapter.setScope(scope);
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void returnScope(ScopeBean scopeBean) {
        mScopeBean = scopeBean;
//        if (header != null) {
//            if (online_header_scopsse != null) {
//                online_header_scopsse.setText(scopeBean.getRealAllScore() + "");
//            }
//        }
        scope = scopeBean.getRealAllScore();
        mPresenter.getMyStudyList(page, 15);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
        if (mPresenter != null) {
            mPresenter.release();
        }

    }

    TextView online_header_scopsse;
    View header;

    private void setHeaderView(RecyclerView view) {
        //View header = LayoutInflater.from(mContext).inflate(R.layout.header_my_study, view, false);
         header = LayoutInflater.from(mContext).inflate(R.layout.header_online_study, view, false);
         online_header_scopsse = (TextView) header.findViewById(R.id.online_header_scopsse);
        TextView online_header_scopsse_rule = (TextView) header.findViewById(R.id.online_header_scopsse_rule);
        LinearLayout online_header_scopsse_ranking = (LinearLayout) header.findViewById(R.id.online_header_scopsse_ranking);
        online_header_scopsse.setText(String.valueOf(mScopeBean.getRealAllScore()));

        /**
         * 跳转积分规则
         */
        online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key", 1)
                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule")
                        .withString("title", "江苏中越科技")
                        .withString("name", "测试")
                        .navigation();
            }
        });


        /**
         * 跳转积分排行
         */
        online_header_scopsse_ranking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key", 1)
                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRank")
                        .withString("title", "江苏中越科技")
                        .withString("name", "测试")
                        .navigation();
            }
        });
    }

    private void setFooterView(RecyclerView view) {
//        View footer = LayoutInflater.from(mContext).inflate(R.layout.footer, view, false);
//        wrapperListAdapter.setFooterView(footer);
    }
}
